//
//  ClaendarTool.m
//  CalendarAndLineLayout
//
//  Created by 骆驼鱼 on 16/11/10.
//  Copyright © 2016年 毒药_YX. All rights reserved.
//

#import "ClaendarTool.h"

@interface ClaendarTool ()

+(BOOL)isLoopYear:(NSInteger)year;

@end
@implementation ClaendarTool

//返回当前年
+(NSInteger)getYear{
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  
  NSDate *nowDate = [NSDate date];
  NSDateComponents *nowDateComps = [[NSDateComponents alloc] init];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  nowDateComps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
  
  return [nowDateComps year];
}

//返回当前月
+(NSInteger)getMonth{
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  
  NSDate *nowDate = [NSDate date];
  NSDateComponents *nowDateComps = [[NSDateComponents alloc] init];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  nowDateComps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
  
  return [nowDateComps month];
}

//返回当前日
+(NSInteger)getDay{
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  
  NSDate *nowDate = [NSDate date];
  NSDateComponents *nowDateComps = [[NSDateComponents alloc] init];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  nowDateComps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
  
  return [nowDateComps day];
}

//获得某个月的第一天是星期几
+(NSInteger)getWeekOfFirstDayOfMonth:(NSInteger)year withMonth:(NSInteger)month{
  
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  
  NSString *firstWeekDayMonth = [[NSString alloc] initWithFormat:@"%ld",(long)year];
  firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%s","-"]];
  firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%ld",(long)month]];
  firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%s","-"]];
  firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%d",1]];
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  NSDate *weekOfFirstDayOfMonth = [dateFormatter dateFromString:firstWeekDayMonth];
  
  NSDateComponents *newCom = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:weekOfFirstDayOfMonth];
  
  return  [newCom weekday] - 1;
}

//返回一个月有多少天
+(NSInteger)getDaysOfMonth:(NSInteger)year withMonth:(NSInteger)month{
  
  NSInteger days = 0;
  //1,3,5,7,8,10,12
  NSArray *bigMoth = [[NSArray alloc] initWithObjects:@"1",@"3",@"5",@"7",@"8",@"10",@"12", nil];
  //4,6,9,11
  NSArray *milMoth = [[NSArray alloc] initWithObjects:@"4",@"6",@"9",@"11", nil];
  
  if ([bigMoth containsObject:[[NSString alloc] initWithFormat:@"%ld",(long)month]]) {
    days = 31;
  }else if([milMoth containsObject:[[NSString alloc] initWithFormat:@"%ld",(long)month]]){
    days = 30;
  }else{
    if ([self isLoopYear:year]) {
      days = 29;
    }else
      days = 28;
  }
  return days;
}

//判断是否是闰年
+(BOOL)isLoopYear:(NSInteger)year{
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    return true;
  }else
    return NO;
}

@end
